package com.yhsoft.common.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author: yyyyyyyh
 * @create: 2023-10-04
 * @Version 1.0
 **/
public class DateUtils {
    public static final String FORMAT_ALL = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_ALL_NOT_SECOND = "yyyy-MM-dd HH:mm";
    public static final String FORMAT_MM_dd = "MM-dd";
    public static final String FORMAT_MM_dd_HH_mm = "MM-dd HH:mm";
    public static final String FORMAT_DEFAULT = "yyyy-MM-dd";
    public static final String FORMAT_MONTH = "yyyy-MM";
    public static final String FORMAT_SPRIT_SHORT = "yyyy/MM/dd";
    public static final String FORMAT_NOT_BLANK = "yyyyMMddHHmmss";
    public static final String FORMAT_DEFAULT_NOT_BLANK = "yyyyMMdd";
    public static final String FORMAT_HOUR_MINUTES = "HH:mm";
    public static final String DEFAULT_TIME = "1900-01-01 00:00:00";

    /**
     * localDateTime -> string
     *
     * @param time
     * @param pattern
     * @return
     */
    public static String localDateTimeToStr(LocalDateTime time, String pattern) {
        if (time == null) {
            return null;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
        return df.format(time);
    }

    /**
     * localDateTime -> string
     *
     * @param time
     * @return
     */
    public static String localDateTimeToStr(LocalDateTime time) {
        if (time == null) {
            return "";
        }
        return localDateTimeToStr(time, FORMAT_ALL);
    }


    /**
     * localDateTime -> string
     *
     * @param time
     * @param pattern
     * @return
     */
    public static String localDate2Str(LocalDate time, String pattern) {
        if (time == null) {
            return null;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
        return df.format(time);
    }

    /**
     * localDateTime -> string
     *
     * @param time
     * @return
     */
    public static String localDate2Str(LocalDate time) {
        if (time == null) {
            return null;
        }
        return localDate2Str(time, FORMAT_DEFAULT);
    }

    public static LocalDateTime localDateToLocalDateTime(LocalDate ld) {
        return ld.atTime(0, 0, 0);
    }

    /**
     * 获取YYYY-MM-DD HH:mm:ss格式
     *
     * @return
     */
    public static String getTime() {
        SimpleDateFormat sdfTime = new SimpleDateFormat(FORMAT_ALL);
        System.out.println("时间戳：" + sdfTime.format(new Date()));
        return sdfTime.format(new Date());
    }

    /**
     * 获取YYYY-MM-DD HH:mm格式
     *
     * @return
     */
    public static String getTimeNoSS() {
        SimpleDateFormat sdfTime = new SimpleDateFormat(FORMAT_ALL_NOT_SECOND);
        System.out.println("时间戳：" + sdfTime.format(new Date()));
        return sdfTime.format(new Date());
    }
}
